// 01login/login.js
var co = require("../resource/js/common.js")
var Map = require("../resource/js/map.js")
var M = require("../resource/js/md5.js")
var app = getApp()
var _this, _data
var interval = null //倒计时函数
Page({
   data: {
      isThis: 0, //切换登录方式
      virifyCodeText: '获取验证码', // 短信验证码
      isUserName: '', // 登录方式错误提示信息
      isPassWord: '',
      isPhoneNumber: '',
      isVerificationCode: '',
      currentTime: 60,
      smsCode: '',
      phoneNumber: '', // 输入的手机号
      tempPhone: '',
      loginInfo: '',
      // 地理位置
      wd: '',
      jd: ''
   },
   onLoad: function(options) {
      _this = this
      _data = this.data
      _this.getLocation()
      _this.setData({
         loginInfo: wx.getStorageSync('loginInfo'),
         login_phoneNumber: wx.getStorageSync('login_phoneNumber'),
      })
   },
   //一打开登录页面获取金纬度
   getLocation() {
      Map.wxGetLocation(function(res){
         console.log("地理位置",res)
         _data.wd = res.latitude
         _data.jd = res.longitude
      })
   },
   // 点击切换登录
   changeContent(e) {
      _this.setData({
         isThis: e.currentTarget.id
      })
   },
   //微信授权登录
   getWxOpenInfo(e) {
      var session = wx.getStorageSync('session')
      if (co.empty(session.session_key)) {
         app.wxLogin(function(res) {
            console.log("【app.wxLogin登录结果】===", res)
            _this.login(res)
         })
         return
      }
      if (co.empty(e))
         return
      app.decryptWxInfoData(e.detail, function(res) {
         _this.login(res)
      })
   },
   //自动登录用户
   login(obj) {
      co.getShareApi('jyhs/Usermg/userAutoLogin', {
         unionid: obj.unionid,
         openid: obj.openid, //因为换appid，需要更新Wx_Openid
         User_PrimaryLng: _data.jd ? _data.jd : 0,
         User_PrimaryLat: _data.wd ? _data.wd : 0
      }, function(res) {
         var D = res.data
         if (res.code == 0) {
            _this.goto(res.data)
            return
         }
      })
   },
   //跳转到首页
   goto(D) {
      app.Data.userInfo = D
      if (D.User_Type == "jyhsb") {
         wx.switchTab({
            url: '/02order/order',
         })
      } else if (D.User_Type == "jyhs_admin" ) {
         wx.navigateTo({
            url: '/04jyhs/admin/home',
         })
      } else if (D.User_Type == "jyhs_admin_worker") {
         wx.navigateTo({
            url: '/04jyhs/admin/home',
         })
      } else {
         co.toastAllText('您的账号审核不通过,请重新注册')
      }
   },
   // 确认登录
   confirmLogin(e) {
      var et = e.detail.value //form携带值为
      if (co.empty(et.phoneNumber))
         wx.setStorageSync('loginInfo', et)
      else {
         wx.setStorageSync('login_phoneNumber', et.phoneNumber)
      }

      var id = _data.isThis //登录方式id
      var smsCode = _data.smsCode //获取到的短信验证码
      var tmPhone = _data.tempPhone //获取验证码的手机码
      var USN = et.userName //form提交用户名
      var PSW = et.passWord //form提交密码
      var PHN = et.phoneNumber //from提交手机号
      var VCO = et.verificationCode //from提交验证码
      var isUserName = ''
      var isPassWord = ''
      var isPhoneNumber = ''
      var isVerificationCode = ''
      if (id == 0) {
         if (USN == '') {
            isUserName = '用户名必填'
         }
         if (PSW == '') {
            isPassWord = '密码必填'
         }
         if (USN == '' || PSW == '') {
            _this.setData({
               isUserName: isUserName,
               isPassWord: isPassWord
            })
            return
         }
         var param = {
            User_Mobile: USN,
            User_Password: M.md5(PSW),
            User_PrimaryLng: _data.jd ? _data.jd : 0,
            User_PrimaryLat: _data.wd ? _data.wd : 0
         }
         _this.MobileLogin(param)
      } else {
         if (PHN == '') {
            isPhoneNumber = '手机号码必填'
         }
         if (VCO == '') {
            isVerificationCode = '验证码必填'
         }
         if (PHN == '' || VCO == '') {
            _this.setData({
               isPhoneNumber: isPhoneNumber,
               isVerificationCode: isVerificationCode
            })
            return
         }
         // 判断验证码是否相等
         if (VCO != smsCode) {
            isVerificationCode = '验证码错误'
         } else if (tmPhone != PHN) {
            isVerificationCode = '验证码已过期'
         } else {
            var param = {
               User_Mobile: PHN,
               Sms_Code: VCO,
               User_PrimaryLng: _data.jd ? _data.jd : 0,
               User_PrimaryLat: _data.wd ? _data.wd : 0
            }
            _this.MobileLogin(param)
         }
         _this.setData({
            isVerificationCode: isVerificationCode
         })
      }
   },
   //手机登录
   MobileLogin(param) {
      co.getShareApi("jyhs/Usermg/userLogin", param, function(res) {
         if (res.code == 0) {
            if (res.data.User_Type == "jyhs_admin") {
               _this.getBizInfo()
            }
            _this.goto(res.data)
         }
      })
   },
   //获取商家信息
   getBizInfo() {
      co.getShareApi("jyhs/Admin/getInfo", {
         Biz_ID: 91
      }, function(res) {
         if (res.code == 0) {
            app.Data.shopInfo = res.data
         }
      })
   },
   // 获取手机号
   getPhoneNumber(e) {
      var phone = e.detail.value //输入的手机号码存入
      _this.setData({
         phoneNumber: phone
      })
   },
   // 获取验证码
   getVerCode() {
      var inPhone = _data.phoneNumber //输入手机号
      var telRule = /^1[3|4|5|7|8]\d{9}$/ //手机号码格式
      var isPhoneNumber = ''
      if (inPhone == '') {
         co.toastAllText("请输入手机号码!")
      } else if (!telRule.test(inPhone)) {
         isPhoneNumber = '手机号码格式错误'
      } else {
         _this.getCode();
         _this.setData({
            disabled: true
         })
         co.getShareApi('api/Sms/getPhoneCode', {
            mobile: _data.phoneNumber,
            type: 'quick_login',
            appid: '1400134010'
         }, function(res) {
            // console.log("【获取返回数据】===", res.data)
            _this.setData({
               smsCode: res.data.Sms_Code
            })
         })
      }
      _this.setData({
         isPhoneNumber: isPhoneNumber,
         tempPhone: inPhone
      })
   },
   /*获取短信验证码倒计时*/
   getCode(options) {
      var currentTime = _data.currentTime
      interval = setInterval(function() {
         currentTime--;
         _this.setData({
            virifyCodeText: currentTime + '秒'
         })
         if (currentTime <= 0) {
            clearInterval(interval)
            _this.setData({
               virifyCodeText: '重新发送',
               currentTime: 60,
               disabled: false,
               smsCode: '',
            })
         }
      }, 1000)
   },
   //跳转至注册页
   goToreRegister() {
      wx.navigateTo({
         url: './reg/regJyhsb',
      })
   },
})